package modellayer;

import java.util.ArrayList;

/**
 * The Class OrderContainer.
 *@author Benjamin Ørskov
 *@author Lasse Lambertsen
 *@author Ole Kirk
 *@author Søren Søgaard
 */
public class OrderContainer {

	/** The instance. */
	private static OrderContainer instance;

	/** The orders. */
	private ArrayList<Order> orders;

	/** The order id. */
	private int orderID = 1;

	/**
	 * Instantiates a new order container.
	 */
	private OrderContainer() {
		orders = new ArrayList<Order>();
	}

	/**
	 * Gets the single instance of OrderContainer.
	 *
	 * @return single instance of OrderContainer
	 */
	public static OrderContainer getInstance() {
		if (instance == null) {
			instance = new OrderContainer();
		}
		return instance;
	}

	/**
	 * Find order.
	 *
	 * @param orderID
	 *            the order id
	 * @return the order
	 */
	public Order findOrder(int orderID) {
		Order returnOrder = null;
		boolean found = false;
		int size = orders.size();
		for (int i = 0; i < size && !found; i++) {
			if (orders.get(i).getOrderID() == (orderID)) {
				returnOrder = orders.get(i);
				found = true;
			}
		}
		return returnOrder;
	}

	/**
	 * Removes the order.
	 *
	 * @param orderID
	 *            the order id
	 */
	public void removeOrder(int orderID) {
		Order tempO = findOrder(orderID);
		orders.remove(tempO);
	}

	/**
	 * Sets the order id.
	 */
	public void setOrderID() {
		orderID--;
	}

	/**
	 * Removes the part order.
	 *
	 * @param o
	 *            the o
	 * @param type
	 *            the type
	 */
	public void removePartOrder(Order o, String type) {
		boolean found = false;
		int size = o.getPartOrders().size();
		for (int index = 0; index < size && !found; index++) {
			if (o.getPartOrders().get(index).getItem().getType().equals(type)) {
				o.removePartOrder(o.getPartOrders().get(index));
				found = true;
			}
		}
	}

	/**
	 * Creates the order.
	 *
	 * @return the order
	 */
	public Order createOrder() {
		Order o = new Order(orderID);
		orders.add(o);
		orderID++;
		return o;
	}

	/**
	 * Sets the salesman.
	 *
	 * @param o
	 *            the o
	 * @param s
	 *            the s
	 */
	public void setSalesman(Order o, Salesman s) {
		o.setSalesman(s);
	}

	/**
	 * Sets the customer.
	 *
	 * @param o
	 *            the o
	 * @param c
	 *            the c
	 */
	public void setCustomer(Order o, Customer c) {
		o.setCustomer(c);
	}
}